﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ScenarioFramework.Portal.Modules.Menu;

namespace ScenarioFramework.Portal.SF.Forms.UserControls
{
    public partial class TreeVerticalMenu : System.Web.UI.UserControl
    {
        public string Menu = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            tvMenus.Nodes.Clear();
            
            MenuHelper menuHelper = (MenuHelper)Session["MenuHelper"];

            string[] vMenus = Menu.Split('/');

            ScenarioFramework.Portal.Modules.Menu.BO.Menu sfMenu = null;
            ScenarioFramework.Portal.Modules.Menu.BO.Menu[] menus = menuHelper.RootMenu.Menu;
            
            foreach (string sMenu in vMenus)
            {
                sfMenu = menuHelper.Get (menus, sMenu);
                menus = sfMenu.Menus;
            }

            if (sfMenu.Menus.Length == 0)
                return;

            TreeNode root = new TreeNode();
            root.Text = sfMenu.Name;

            //foreach (SFMenu menu in sfMenu.Menus)
            foreach (ScenarioFramework.Portal.Modules.Menu.BO.Menu menu in sfMenu.Menus)
            {
                this.AddMenuToTree(ref root, menu);
                
            }

            this.tvMenus.Nodes.Add(root);
        }

        private void AddMenuToTree(ref TreeNode root, ScenarioFramework.Portal.Modules.Menu.BO.Menu menu)
        {
            TreeNode node = new TreeNode();
            node.Text = menu.Name;
            node.NavigateUrl = menu.Url;

            root.ChildNodes.Add(node);

            if ((menu.Menus != null) && (menu.Menus.Length > 0))
            {
                foreach (ScenarioFramework.Portal.Modules.Menu.BO.Menu subMenu in menu.Menus)
                {
                    this.AddMenuToTree(ref node, subMenu);
                }
            }
        }
    }
}